// $(function () {
//     $.ajax({
//         type : "GET",
//         url : "/bbs/getTopicList.action",
//         data : {
//             method : "query"
//         },
//         dataType : "json",//返回的数据类型
//
//         success : function(data) {
//             if(data.code == 200){
//                 var articleList = data.data;
//                 var html = '';
//                 for ( var i = 0; i < articleList.length; i++) {//循环json对象，拼接tr,td的html
//                     html = html + '<tr>';
//                     html = html + '<td width="50px" id="articleNum"><div style="width:50px;word-wrap:break-word;">' + (i + 1) + '</div></td>';
//                     html = html + '<td width="300px" id="articleTitles"><div style="width:300px;word-wrap:break-word;"><a href="javascript:void(0);">' + articleList[i].t_title + '</a></div></td>';
//                     html = html + '<td id="articleUsername">' + articleList[i].t_author_id + '</td>';
//                     html = html + '<td id="articleBname">' + articleList[i].b_id + '</td>';
//                     html = html + '<td id="articleCreateTime">' + articleList[i].t_date + '</td>';
//                     html = html + '<td id="articleUpdateTime">' + articleList[i].t_last_date + '</td>';
//                     html = html + '<td id="articleUpdateTime"><span id="rank_articleSum" class="badge" style="background-color: #FFCC33;">' + articleList[i].t_comment_num + '</span></td>';
//                     html = html + '</tr>';
//                 }
//                 $('#listArticle_hide').html(html);//通过jquery方式获取table，并把tr,td的html输出到table中
//             }else{
//                 alert(500);
//             }
//         },
//         error : function() {
//             alert("查询失败！");
//         }
//     });
// })
//

$(function () {
    layui.use(['jquery', 'table'], function () {
        var table = layui.table;
        var $ = layui.jquery;
        //渲染表格
        table.render({
            elem: "#articleTable",//渲染的表格的id
            url: "/bbs/getTopicListByPage.action",//请求后台的接口
            cols: [[//表头
                {type:'numbers',title:'编号',width:90,align:'center'},
                {field:'t_title',title:'文章标题',width:190,align:'center'},
                {field:'t_author_id',title:'作者ID', width:120,align:'center'},
                {field:'b_id',title:'所属版块',width:120,align:'center'},
                {field:'t_date',title:'创建时间',width:180,align:'center'},
                {field:'t_last_date',title:'最后更新时间',width:180,align:'center'},
                {field:'t_comment_num',title:'评论数',width:180,align:'center'},
                {title:'操作',toolbar:'#userOperation',width:180,align:'center'},
            ]],
            page: false//是否开启分页
        });

        //表格的重载
        function reloadArticleTable(){
            table.reload("articleTable",{
                where:{
                    t_title:$("t_title").val(),
                    t_author_id:$("t_author_id").val(),
                    b_id:$("b_id").val(),
                    t_date:$("t_date").val(),
                    t_last_date:$("t_last_date").val(),
                    t_comment_num:$("t_comment_num").val(),
                }
            },'data');
        }

        //监听表格的工具栏的事件
        table.on('tool(articleTable)',function (obj) {
            var data = obj.data;
            if(obj.event == 'deleteArticle'){
                alert("删除");
                deleteArticle(data.t_id);
                //changeUserType(data.u_id);
            }
        });

        //删除公告
        function deleteArticle(t_id){
            $.ajax({
                url:"/bbs/removeTopicById.action",
                type:"POST",
                data:{'t_id':t_id},
                dataType:'JSON',
                cache:false,
                success:function (data) {
                    if(data.code==200){
                        alert("删除成功!");
                        //重载表格
                        reloadArticleTable();
                    }else{
                        alert(data.msg);
                    }
                } ,
                error:function () {
                    alert("删除失败.")
                }
            });
        };
    });
})
